<template>
  <div v-if="showTinymceEditor">
    <editor
      v-if="showTinymceEditor"
      id="tinymceEditor"
      v-model="contentValue"
      :init="init"
    ></editor>
  </div>
</template>
<script lang="ts">
import {defineComponent, onUnmounted, reactive, ref, watch} from "vue";
import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
import "tinymce/themes/silver"; //编辑器主题，不引入则报错
import "tinymce/icons/default"; //引入编辑器图标icon，不引入则不显示对应图标

// 引入编辑器插件（基本免费插件都在这儿了）
import "tinymce/plugins/advlist"; //高级列表
import "tinymce/plugins/anchor"; //锚点
import "tinymce/plugins/autolink"; //自动链接
import "tinymce/plugins/autoresize"; //编辑器高度自适应,注：plugins里引入此插件时，Init里设置的height将失效
import "tinymce/plugins/autosave"; //自动存稿
import "tinymce/plugins/charmap"; //特殊字符
import "tinymce/plugins/code"; //编辑源码
import "tinymce/plugins/codesample"; //代码示例
import "tinymce/plugins/directionality"; //文字方向
import "tinymce/plugins/emoticons"; //表情
import "tinymce/plugins/fullpage"; //文档属性
import "tinymce/plugins/fullscreen"; //全屏
import "tinymce/plugins/help"; //帮助
import "tinymce/plugins/hr"; //水平分割线
import "tinymce/plugins/image"; //插入编辑图片
import "tinymce/plugins/importcss"; //引入css
import "tinymce/plugins/insertdatetime"; //插入日期时间
import "tinymce/plugins/link"; //超链接
import "tinymce/plugins/lists"; //列表插件
import "tinymce/plugins/media"; //插入编辑媒体
import "tinymce/plugins/nonbreaking"; //插入不间断空格
import "tinymce/plugins/pagebreak"; //插入分页符
import "tinymce/plugins/paste"; //粘贴插件
import "tinymce/plugins/preview"; //预览
import "tinymce/plugins/print"; //打印
import "tinymce/plugins/quickbars"; //快速工具栏
import "tinymce/plugins/save"; //保存
import "tinymce/plugins/searchreplace"; //查找替换
import "tinymce/plugins/tabfocus"; //切入切出，按tab键切出编辑器，切入页面其他输入框中
import "tinymce/plugins/table"; //表格
import "tinymce/plugins/template"; //内容模板
import "tinymce/plugins/textcolor"; //文字颜色
import "tinymce/plugins/textpattern"; //快速排版
import "tinymce/plugins/toc"; //目录生成器
import "tinymce/plugins/visualblocks"; //显示元素范围
import "tinymce/plugins/visualchars"; //显示不可见字符
import "tinymce/plugins/wordcount"; //字数统计

import {EditorProps} from "./interface";
import {upload} from "@/api/common";

export default defineComponent({
  name: "Tinymce",
  components: {
    Editor
  },
  props: EditorProps,
  emits: ["update:content"],
  setup(props, {emit}) {
    const {plugins, toolBar, menuBar, height, content} = props;
    const init = reactive({
      language_url:
        import.meta.env.BASE_URL + "resource/tinymce/langs/zh-Hans.js",
      language: "zh-Hans",
      skin_url: import.meta.env.BASE_URL + "resource/tinymce/skins/ui/oxide", //皮肤：浅色
      content_css:
        import.meta.env.BASE_URL +
        "resource/tinymce/skins/content/default/content.css", //以css文件方式自定义可编辑区域的css样式，css文件需自己创建并引入
      height: height,
      plugins: plugins,
      toolbar: toolBar,
      menubar: menuBar,
      branding: false,
      // 图片上传三个参数，图片数据，成功时的回调函数，失败时的回调函数
      images_upload_handler: function (blobInfo, success, failure) {
        let formData = new FormData();
        // 服务端接收文件的参数名，文件数据，文件名
        formData.append("image", blobInfo.blob(), blobInfo.filename());
        upload(formData)
          .then((res) => {
            success(res.data.url);
          })
          .catch(() => {
            failure("上传失败");
          });
      }
    });

    const showTinymceEditor = ref(true);
    const contentValue = ref(content);
    watch(contentValue, (val) => {
      emit("update:content", val);
    });

    onUnmounted(() => {
      showTinymceEditor.value = false;
    });

    return {
      init,
      contentValue,
      tinymce,
      showTinymceEditor
    };
  }
});
</script>
<style lang="less" scoped></style>
